package com.zsj.controller;

import com.zsj.entity.WebResponse;
import com.zsj.enums.ErrorCodeEnum;
import com.zsj.job.StatisticsJob;
import com.zsj.service.BatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Description:
 * @Author:
 * @Date:
 */
@RestController
@RequestMapping("/batch")
@Slf4j
public class BatchController {

    @Autowired
    private BatchService batchService;

    @Autowired(required = false)
    private StatisticsJob statisticsJob;

    @PostMapping(value = "/createFile")
    public WebResponse createFile(@RequestParam(value = "date") String date) {
        log.info("开始生成对账文件，日期:{}", date);
        try {
            boolean flag = batchService.createFile(date);
            if (flag) {
                log.info("生成{}对账文件成功", date);
            } else {
                log.info("生成{}对账文件失败", date);
            }
        } catch (Exception e) {
            log.error("生成对账文件异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
        return WebResponse.success();
    }

    @PostMapping(value = "/quality_statistics")
    public WebResponse qualityStatistics(@RequestParam(value = "date") String date) {
        log.info("开始生成统计数据，日期:{}", date);
        if (Objects.isNull(statisticsJob)) {
            return WebResponse.success();
        }
        try {
            statisticsJob.qualityStatistics(date);
            log.info("生成统计文件成功", date);
        } catch (Exception e) {
            log.error("生成统计文件异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
        return WebResponse.success();
    }
}
